import {Math, Scene} from 'phaser';

import {EVENTS_NAME} from '../consts'
import {Actor} from "./actor";
import {Player} from "./player"

//设置定时器让怪物自动走动
enum Direction {
  UP,
  DOWN,
  LEFT,
  RIGHT,
}

// 随机生成不同的方向
const randomDirection = (exclude: Direction) => {
  let newDirection = Phaser.Math.Between(0, 3) //生成指定范围内随机整数，0,1,2,3
  while (newDirection === exclude) {
    newDirection = Phaser.Math.Between(0, 3)
  }
  return newDirection
}

export class Enemy extends Actor {
  private direction = Direction.RIGHT
  private moveEvent: Phaser.Time.TimerEvent

  private target: Player
  private AGRESSOR_RADIUS = 100;
  private attackHandler: () => void

  constructor(
    scene: Scene,
    x: number,
    y: number,
    texture: string,
    target: Player,
    frame?: string | number,
  ) {
    // super(scene, x, y, "king"); 怪物会变成人
    super(scene, x, y, texture, frame);
    this.target = target

    // ANIMATIONS
    this.anims.play('lizard-idle')

    //给怪物添加事件监听，当怪物和角色的距离小于角色的宽度，说明击中
    this.attackHandler = () => {
      if (Math.Distance.BetweenPoints(
        {x: this.x, y: this.y},
        {x: this.target.x, y: this.target.y}
      ) < this.target.width) { //this.target.width 角色的宽度
        this.getDamage()//获取伤害方法
        this.disableBody(true, false) //停止怪物对象主体，但不消失

        this.scene.time.delayedCall(300, () => {
          this.destroy() //300ms 后消失
        })
      }
    }
    //EVENTS
    this.scene.game.events.on(EVENTS_NAME.attack, this.attackHandler, this)
    //销毁后取消监听
    this.on('destroy', () => {
      this.scene.game.events.removeListener(
        EVENTS_NAME.attack,
        this.attackHandler
      )
    })

    //每隔2秒改变怪物行走的方向
    this.moveEvent = scene.time.addEvent({
      delay: 2000,
      callback: () => {
        this.direction = randomDirection(this.direction);
      },
      loop: true
    })
  }

  destroy(fromScene?: boolean) {
    this.moveEvent.destroy() //清理与moveEvent相关的资源

    super.destroy(fromScene)  // 调用父类的 destroy 方法
  }

  // 怪物自动走动、跑的逻辑
  preUpdate(t: number, dt: number) {
    // console.log('怪物-preUpdate', t, dt)
    super.preUpdate(t, dt);
    if (
      Math.Distance.BetweenPoints(
        {x: this.x, y: this.y},
        {x: this.target.x, y: this.target.y}
      ) < this.AGRESSOR_RADIUS
    ) {
      //距离小于100，设置朝英雄一个速度
      this.getBody().setVelocityX(this.target.x - this.x) //设置在x轴上的速度
      this.getBody().setVelocityY(this.target.y - this.y)
    } else {
      //大于100，随机几个方向自动走动
      const speed = 50
      switch (this.direction) {
        case Direction.UP:
          this.getBody().setVelocity(0, -speed)
          break
        case Direction.DOWN:
          this.getBody().setVelocity(0, speed)
          break
        case Direction.LEFT:
          this.getBody().setVelocity(-speed, 0)
          break
        case Direction.RIGHT:
          this.getBody().setVelocity(speed, 0)
          break
      }
    }
  }

  update() {
  }

  public setTarget(target: Player) {
    this.target = target
  }
}


